var numList = [];
for (var i = 48; i <= 57; i++) {
    var char = String.fromCharCode(i)
    numList.push(char);
}

var smallList = [];
for (var i = 97; i <= 122; i++) {
    var char = String.fromCharCode(i)
    smallList.push(char);
}

var bigList = [];
for (var i = 65; i <= 90; i++) {
    var char = String.fromCharCode(i)
    bigList.push(char);
}

var speList = ["_", "$"]

var list = numList.concat(smallList, bigList, speList)
var pwdList = numList.concat(smallList, bigList)

function randomNumber(n) {
    n = n || 4
    var num = ''
    for (i = 1; i <= n; i++) {
        num += pwdList[Math.round(Math.random() * 61)]
    }
    return num
}

function randColor() {
    var str = "#"
    for (var i = 0; i < 6; i++) {
        str += (Math.round(Math.random() * 16)).toString(16)
    }
    return str;
}

/*------------------------------------------------------------------------------------------------------------------------------------------*/
//判断用户名字是否正确
function judgeUserName(user) {
    if (user.length >= 6 && user.length <= 12) {
        if (isNaN(user.charAt(0))) {//判断首字母是否为数字
            var flag = true
            for (var i = 0; i < user.length; i++) {
                var char = user.charAt(i)
                if (!list.includes(char)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                spantext(userSpan, '用户名ok', true)
            } else {
                spantext(userSpan, '用户名不能包含非法字符哦')
                return 0
            }
        }
        else {
            spantext(userSpan, '用户名不能以数字开头哦')
            return 0
        }
    }
    else {
        spantext(userSpan, '用户名长度不合法哦')
        return 0
    }
    return 1 * 1
}
//判断密码输入是否合法
function judgePassWorld(pwd) {
    if (pwd.length >= 6 && pwd.length <= 12) {
        var flag = true
        for (i = 0; i < pwd.length; i++) {
            if (!pwdList.includes(pwd.charAt(i))) {
                flag = false
                break
            }
        }
        if (flag) {
            spantext(pwdSpan, '密码输入正确', true)
            //循环遍历判断密码强度
            var flag1 = 0, flag2 = 0, flag3 = 0, flag = 0
            for (i = 0; i < pwd.length; i++) {
                //判断密码是否含有数字
                if (numList.includes(pwd[i])) {
                    flag1 = true
                }
                //判断密码是否含有小写字母
                if (smallList.includes(pwd[i])) {
                    flag2 = true
                }
                //判断密码是否含有大写字母
                if (bigList.includes(pwd[i])) {
                    flag3 = true
                }
            }
            //flag来判断密码强度
            switch (flag1 + flag2 + flag3) {
                case 1: flag = '弱'
                    break
                case 2: flag = '中'
                    break
                case 3: flag = '强'
                    break
            }
            spantext(pwdSpan, '密码强度为' + flag, true)
        } else {
            spantext(pwdSpan, '密码含有非法字符哦')
            return 0
        }
    } else {
        spantext(pwdSpan, '密码长度不合法哦')
        return 0
    }
    return 1 * 1
}
//判断再次输入的密码是否合法
function judgeRePassWorld(repwd) {
    var pwd = document.getElementById('pwd').value
    if (repwd == pwd && repwd != '') {
        spantext(repwdSpan, '密码一致', true)
    } else {
        spantext(repwdSpan, '两次密码不一致，请重新输入')
        return 0
    }
    return 1 * 1
}
//判断验证码是否正确
function judgeCode(code) {
    var randomCode = document.getElementById('random_code').textContent
    if (code.toLowerCase() == randomCode.toLowerCase()) {
        spantext(codeSpan, '验证码正确', true)
    } else {
        document.getElementById('random_code').innerHTML = randomNumber(4)
        spantext(codeSpan, '验证码错误，请重新输入')
        //输入错误把验证码填写清空
        document.getElementById('code').value = ''
        //换个颜色儿
        randomCode.style.color = randColor()
        return 0
    }
    return 1 * 1
}
//输出语句
function spantext(element, str, boolean) {
    boolean = boolean || false
    element.innerHTML = str
    element.style.color = boolean ? 'green' : 'red'
}
//生成随机验证码
function setRanCode(randomCode) {
    randomCode.innerHTML = randomNumber(4)
    randomCode.style.color = randColor()
}

//--------------------------------------------------------------------------------------------------------------------
function setCookie(key, val, expires, path = "/") {
    if (expires) {
        var date = new Date()
        date.setSeconds(date.getSeconds() + expires);
        document.cookie = key + "=" + val + "; expires=" + date.toUTCString() + "; path=" + path
    } else {
        document.cookie = key + "=" + val + "; path=" + path
    }
}


function getCookie(key) {
    var cookie = document.cookie
    if (cookie) {
        var list = cookie.split("; ")
        for (var i = 0; i < list.length; i++) {
            var item = list[i]
            var name = item.split("=")[0]
            var val = item.split("=")[1]
            if (name === key) {
                return val
            }
        }
    }
    return ''
}


function delCookie(key) {
    setCookie(key, "", -1)
}
//------------------------------------------------------------------------------------------------------
function ajax(ajaxObject) {
    var { type = 'get', url, data = '', async = true, dataType = "text", success } = ajaxObject
    var xhr = new XMLHttpRequest()
    if (typeof data == 'object') {
        var dataList = []
        for (var key in data) {
            dataList.push(key + '=' + data[key])
        }
        data = dataList.join('&')
    }
    if (type.toLowerCase() == 'get') {
        xhr.open('get', data ? url + '?' + data : url, async)
        xhr.send()
    } else if (type.toLowerCase() == 'post') {
        xhr.open('post', url, async)
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
        xhr.send(data)
    }
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var res = xhr.responseText
            if (dataType == 'json') {
                res = JSON.parse(res)
            }
            if (success && typeof success === "function") {
                success(res)
                // console.log(res);
            }
        }
    }
}
function urlParamsParse() {

    var search = location.search;  // ?keyword=%E5%8D%8E%E4%B8%BA&enc=utf-8&wq=%E5%8D%8E%E4%B8%BA&pvid=2594ef2dec5d4ac396b50ef99d4569a5
    var data = {}

    if (search) {
        // 将数据拆分为指定条数  ("&")

        var str = search.substring(1); // 从下标为1 截取到最后

        var list = str.split("&");
        // console.log(list); // ['user=a123123', 'pwd=123123', 'phone=17386151421', 'email=123132@qq.com']

        // 取出每一条的键名和键值  ("=")
        list.forEach(item => {  // 'user=a123123'
            var key = item.split("=")[0];
            var val = item.split("=")[1];
            data[key] = decodeURIComponent(val); // 数据解析成功之后 在对键值解码
        })
    }
    return data;
}


